package com.xjj.serviceattend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.rabbitmq.client.AMQP;
import com.xjj.entity.*;
import com.xjj.serviceattend.feignclient.AttendFeignClient;
import com.xjj.serviceattend.feignclient.KlassFeignClient;
import com.xjj.serviceattend.feignclient.ManagerFeignClient;
import com.xjj.serviceattend.service.AttendService;
import com.xjj.serviceattend.vo.AttendVo;
import com.xjj.util.utils.JwtUtil;
import com.xjj.vo.KlassVo;
import com.xjj.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import static com.xjj.util.utils.JwtUtil.getManager;

/**
 * @Auther: AYang
 * @Date: 2022/10/20 10:51
 * @Description:
 */

@Api(tags = "考勤相关接口")
@RestController
@RequestMapping("/attend/admin")
@Slf4j
public class AttendController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Resource
    AttendFeignClient attendFeignClient;

    @Resource
    ManagerFeignClient managerFeignClient;

    @Resource
    KlassFeignClient klassFeignClient;

    @Autowired
    private AttendService attendService;

    @GetMapping("/getKlassByManagerId")
    public ResponseEntity getKlassByMid(HttpServletRequest request) throws JsonProcessingException {
        Manager manger = getManager(request.getHeader("token"));
        log.debug("老师id:{}"+manger.getId());
        // 老师id
        Integer id = manger.getId();
        return klassFeignClient.getKlassByManagerId(id);
    }

    /**
     * 根据班级id获取今日出勤列表
     * @param
     * @return
     */
    @PostMapping("/getStuByKid")
    public ResponseEntity<List<Student>> getStuByKid(HttpServletRequest request){
        ResponseEntity<Manager> responseEntity = managerFeignClient.getMe(request);
        log.debug("data:{}" + responseEntity.getData());
        // 获取老师id
        Integer id = responseEntity.getData().getId();
        // 通过老师id获取当前班级id
        ResponseEntity<Klass> klass = klassFeignClient.getKlassByManagerId(id);
        Klass data = klass.getData();
        Integer klassId = data.getId();
        List<Student> stuByKid = attendService.getStuByKid(klassId);
        return ResponseEntity.SUCCESS("ok",stuByKid);
    }

    @PostMapping("/list")
    public ResponseEntity<PageBean<Student>> list(@RequestBody StudentVo studentVo){
        log.debug("studentVo:{}" + studentVo);

        return attendFeignClient.list(studentVo);
    }

    @PostMapping("/attendlist")
    public ResponseEntity<PageBean<Attend>> attendList(@RequestBody AttendVo attendVo) {
        // 每页展示条数，暂定 5
        Integer pageSize = 10;
        Attend attend = attendVo.getAttend();
        Integer pageNum = attendVo.getPageNum() == null ? 1 : attendVo.getPageNum();
        PageBean<Attend> pageBean = attendService.getByPage(attend, pageNum, pageSize);
        return ResponseEntity.SUCCESS("获取列表成功", pageBean);
    }

    /**
     * 老师发布考勤，将学生考勤状态统一设置为y(默认全都到期)
     * @param klassId
     * @return
     */
    @PostMapping("/publish/{klassId}")
    public ResponseEntity publish(@PathVariable Integer klassId){
        Student student = new Student();
        student.setKlassId(klassId);
        student.setAttendStatus("y");
        // 根据班级id修改考勤状态
        attendService.updateAttendStatusByKlassId(student.getKlassId(),student.getAttendStatus());
        return ResponseEntity.SUCCESS("ok",null);
    }

    /**
     * 老师发布考勤(缺勤)
     * @param id 学生id
     * @return
     */
    @PostMapping("/absent")
    public ResponseEntity absent(@PathVariable Integer id){
        Student student = new Student();
        student.setId(id);
        student.setAttendStatus("n");
        // 根据学生id修改考勤状态
        attendService.updateAttendStatusBySid(student.getId(),student.getAttendStatus());
        return ResponseEntity.SUCCESS("ok",null);
    }

    /**
     * 老师发布考勤(迟到)
     * @param id 学生id
     * @return
     */
    @PostMapping("/late/{id}")
    public ResponseEntity late(@PathVariable Integer id){
        Student student = new Student();
        student.setId(id);
        student.setAttendStatus("l");
        // 根据学生id修改考勤状态
        attendService.updateAttendStatusBySid(student.getId(),student.getAttendStatus());
        return ResponseEntity.SUCCESS("ok",null);
    }

    /**
     * 通过班级id添加考勤记录
     * @param klassId
     * @return
     */
    @PostMapping("/addRecord/{klassId}")
    public ResponseEntity addRecord(@PathVariable Integer klassId){
        Attend attend = new Attend();
        // 实到人数 统计status为y的学生
        Integer y = attendService.getAttendStatusNum("y",klassId);
        attend.setAttendedNum(y);
        // 缺勤人数 统计status为n的学生
        Integer n = attendService.getAttendStatusNum("n",klassId);
        attend.setAbsenceNum(n);
        // 迟到人数 统计status为l的学生
        Integer l = attendService.getAttendStatusNum("l",klassId);
        attend.setLateNum(l);
        attend.setKlassId(klassId);
        attend.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        rabbitTemplate.convertAndSend("exchangeAttendDelay", "attendDelay", attend);
        // attendService.addRecord(attend);
        return ResponseEntity.SUCCESS("ok","发送死信队列成功");
    }

    private Manager getManager(String token) throws JsonProcessingException {
        System.out.println(token);
        //解析出来Manager
        Manager manager = JwtUtil.getManager(token);
        System.out.println("manager" + manager);
        return manager;
    }
}
